<?php

namespace handle\message\lib\manage\lib;

use handle\message\lib\manage\traits\Observer;

class ChessBoard
{
    use Observer;
    /**
     * 房间ID
     * @var string
     */
    private string $room_id;

    /**
     * 棋盘行数
     * @var int
     */
    private int $line = 15;

    /**
     * 整个棋盘
     * @var array
     */
    private array $chessBoard = [];

    /**
     * 历史棋盘
     * @var array
     */
    private array $history = [];

    /**
     * 回合数
     * @var int
     */
    private int $round = 0;

    /**
     * 初始化棋盘
     */
    public function __construct($room_id){
        $this->room_id = $room_id;
    }

    /**
     * 初始化棋盘
     * @return void
     */
    public function init(){
        $this->makeBoard();
        $this->history = [];
        $this->round = 0;
        $this->notifyManage();
    }

    /**
     * 获取房间ID
     * @return string
     */
    public function getRoomId(): string
    {
        return $this->room_id;
    }
    /**
     * 获取整局棋盘
     * @return array
     */
    public function getBoard(): array
    {
        return $this->chessBoard;
    }

    /**
     * 更新棋盘
     * @param int $x
     * @param int $y
     * @param int $chessColor 棋子颜色
     * @param int $uid
     * @return void
     */
    public function update(int $x, int $y, int $chessColor, int $uid)
    {
        $this->round++; //回合数+1
        $this->chessBoard[$x][$y] = $chessColor;
        $this->history[] = [
            'uid' => $uid,
            'color' => $chessColor,
            'x' => $x,
            'y' => $y,
            'withdraw' => false,
        ];
        $this->notifyManage();
    }
    /**
     * 判断某个位置上是否已经有棋子
     * @param int $x
     * @param int $y
     * @return bool
     */
    public function checkSomeXY(int $x, int $y): bool
    {
        return $this->chessBoard[$x][$y] !== 0;
    }
    /**
     * 悔棋操作
     * @return void
     */
    public function withdraw()
    {
        $this->round--; //回合数-1
        $lastHistory = end($this->history);
        $lastKey = key($this->history);
        $this->history[$lastKey]['withdraw'] = true;
        $this->chessBoard[$lastHistory['x']][$lastHistory['y']] = 0;
        $this->notifyManage();
    }

    /**
     * 获取最后一次历史记录
     * @param string $key
     * @return array|mixed
     */
    public function getLastOneHistory(string $key = '')
    {
        $history = $this->getHistory();
        $lastOne = end($history);
        if(!empty($key)){
            return $lastOne[$key] ?? null;
        }
        return $lastOne;
    }
    /**
     * 获取历史记录
     * @return array
     */
    public function getHistory(): array
    {
        return $this->history;
    }

    /**
     * 获取回合数
     * @return int
     */
    public function getRound(): int
    {
        return $this->round;
    }
    /**
     * 创建一个空棋盘
     * @return void
     */
    private function makeBoard(){
        $this->chessBoard = [];
        for ($i = 0; $i < $this->line; $i++) {
            $this->chessBoard[$i] = [];
            for ($j = 0; $j < $this->line; $j++) {
                $this->chessBoard[$i][$j] = 0;
            }
        }
    }

}